
package com.meviewer.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.meviewer.common.FileUtil;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.net.URL;


public class JsonService {
    String id;
    String stroke;
    String strokeAlpha;
    String strokeWidth;
    String fill;
    String fillAlpha;
    String clampToGround;
    String  path = "storage/emulated/0/mev/data/layer.json";

    String stroke1;
    String fill1;
    int pixeSize;

    public JsonService(){};

    public JsonService(String id,String stroke1,String fill1,int pixelSize){

  /*      String stroke,String strokeAlpha,String strokeWidth,String fill,
                String fillAlpha,*/
        this.id = id;
        /*this.stroke = stroke;
        this.strokeAlpha = strokeAlpha;
        this.strokeWidth = strokeWidth;
        this.fill = fill;
        this.fillAlpha = fillAlpha;*/
        this.stroke1 = stroke1;
        this.fill1 = fill1;
        this.pixeSize = pixelSize;


    }
    public void modifyJson()  {
        try {



            String s = readJsonFile(path);

            JSONArray arr = JSON.parseArray(s);
            JSONObject rowData;
            JSONObject rowData1;

            for (int i = 0; i < arr.size(); i++) {
                rowData = arr.getJSONObject(i);
                System.out.println(rowData.toJSONString());

                JSONArray arr1 = JSON.parseArray(rowData.get("children").toString());




             /*   for (int k = 0; k < arr1.size(); k++) {

                    if(arr1.get(k).toString().equals("properties")){
                        arr1.remove(k);
                    }

                }*/
                for (int j = 0; j < arr1.size(); j++) {
                    rowData1 = arr1.getJSONObject(j);

                    if(rowData1.get("id").toString() == id ||
                            rowData1.get("id").toString() .equals(id)){
                        System.out.println("数据111");



                        JSONObject rowData2 = JSONObject.parseObject(rowData1.get("properties").toString());
                       /* rowData2.put("stroke",stroke);
                        rowData2.put("strokeAlpha",strokeAlpha);
                        rowData2.put("strokeWidth",strokeWidth);
                        rowData2.put("fill",fill);
                        rowData2.put("fillAlpha",fillAlpha);*/
                        rowData2.put("stroke1",stroke1);
                        rowData2.put("fill1",fill1);
                        rowData2.put("pixelSize",pixeSize);
                        rowData1.put("properties",rowData2);


                        System.out.println("马坎纳卡");
                        System.out.println(rowData1);
                        System.out.println("阿萨萨达萨达");


                           arr1.add(rowData1);


                        rowData.put("children",arr1);
                        break;
                    }
                }

            }

          /*  System.out.println("看看 json 对象");
            System.out.println(arr);*/

       /*     String fileText = JSONArray.toJSONString(arr, SerializerFeature.DisableCircularReferenceDetect);*/
            String fileText = arr.toJSONString();
            BufferedWriter writer = new BufferedWriter(new FileWriter(path));
            writer.write(fileText);
            writer.close();
        }catch (Exception e){
            e.printStackTrace();
        }



    }


    ///**
    // * 复制整个文件夹内容
    //
    public  void copyFolder(String oldPath, String newPath) throws Exception {

        // (new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹
        if(oldPath.equals(newPath))
        {
            return;
        }
        File ssspath = new File(newPath);
        if(ssspath.exists())
        {
            return;
        }

        File a = new File(oldPath);

        if (a.isDirectory())
        {
            if(!ssspath.exists())
            {
                ssspath.mkdir();
            }
            String[] file = a.list();
            File temp = null;

            for (int i = 0; i < file.length; i++) {
                if (oldPath.endsWith(File.separator)) {
                    temp = new File(oldPath + file[i]);
                } else {
                    temp = new File(oldPath + File.separator + file[i]);
                }

                if (temp.isFile()) {
                    if(!temp.exists())
                    {
                        temp.createNewFile();
                    }
                    FileInputStream input = new FileInputStream(temp);
                    FileOutputStream output = new FileOutputStream(newPath + "/" +
                            (temp.getName()).toString());
                    byte[] b = new byte[1024 * 5];
                    int len;
                    while ((len = input.read(b)) != -1) {
                        output.write(b, 0, len);
                    }
                    output.flush();
                    output.close();
                    input.close();
                }
                if (temp.isDirectory()) {//如果是子文件夹
                    copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
                }
            }

        }else
        {

            System.out.println("走走走走哦哦在真空空");

            if(!ssspath.exists())
            {
                ssspath.createNewFile();
            }


            FileInputStream input = new FileInputStream(oldPath);
            FileOutputStream output = new FileOutputStream(newPath);
            byte[] KB = new byte[1024];
            int index;
            while ((index = input.read(KB)) != -1) {
                output.write(KB, 0, index);
            }
            output.flush();
            output.close();
            input.close();

        }


    }






    public static String readJsonFile(String fileName) {
        /*  String jsonStr = "";*/
        try {
            File jsonFile = new File(fileName);
            FileReader fileReader = new FileReader(jsonFile);

            Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            String jsonStr = jsonStr = sb.toString();
            return jsonStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }


    public static void testBeytes() {
        InputStream in = null;
        OutputStream out = null;
        try {
            URL url = new URL("http://www.csdn.net/");
            in = url.openStream();
            out = System.out;





            byte[] buffer = new byte[4096];
            int bytes_read;
            while ((bytes_read = in.read(buffer)) != -1) {
                out.write(buffer, 0, bytes_read);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
                out.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }


    /**
     * 删除数据&文件
     */
    public static boolean deleteFolder(String url) {
        File file = new File(url);
        if (!file.exists()) {
            return false;
        }
        if (file.isFile()) {
            file.delete();
            return true;
        } else {
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                String root = files[i].getAbsolutePath();//得到子文件或文件夹的绝对路径
                //System.out.println(root);
                deleteFolder(root);
            }
            file.delete();
            return true;
        }
    }




    public static String jugePath(String layerPath,String path,String datatype){
        int index = layerPath.lastIndexOf("/");
        String m_path = layerPath.substring(0,layerPath.lastIndexOf("/")+1);
            if(datatype.equals("model"))
            {
                m_path =m_path+path;
            }else if(datatype.equals("geojson"))
            {
                m_path =m_path+path;
            }else if(datatype.equals("b3dm"))
            {
                String m_tmpd = path;
                m_tmpd = m_tmpd.substring(0,m_tmpd.lastIndexOf("/")+1);
                m_path  = m_path+m_tmpd;
                System.out.println(m_path );
            }else if(datatype.equals("ImageTiles"))
            {

                String m_tmp = path;
                for(int mm=0 ;mm<3;mm++)
                {
                    int m_tmmm = m_tmp.lastIndexOf("/");
                    m_tmp =m_tmp.substring(0,m_tmmm);
                }
                m_path  = m_path+m_tmp;
            }
            return  m_path;

    }


























}